package com.grj.park.test;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProcessController {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @GetMapping("/start-process")
    public String startProcess() {
        Map<String, Object> variables = new HashMap<>();
        // 添加流程变量
        runtimeService.startProcessInstanceByKey("test", variables);
        return "流程已启动";
    }

    @GetMapping("/complete-task")
    public String completeTask() {
        List<Task> tasks = taskService.createTaskQuery().list();
        for (Task task : tasks) {
//            System.out.println(task.getName());
            if ("销售经理审批".equals(task.getName())) {
                taskService.complete(task.getId());
                System.out.println("销售经理审批任务已完成");
                return "销售经理审批任务已完成";
            } else if ("总经理审批".equals(task.getName())) {
                taskService.complete(task.getId());
                System.out.println("总经理审批任务已完成");
                return "总经理审批任务已完成";
            }
        }
        System.out.println("没有待处理的任务");
        return "没有待处理的任务";
    }
}
